package com.lsk.servicemap.remote;

import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.AMapConfigConstants;
import com.lsk.internalcommon.response.GaodeResponse;
import com.lsk.internalcommon.response.TerminalResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class TerminalClient {
    @Value("${map.key}")
    private String mapKey;

    @Value("${map.sid}")
    private String sid;

    @Autowired
    private RestTemplate restTemplate;
    public ResponseResult add(String name){
        //拼接url
        StringBuilder url = new StringBuilder();
        url.append(AMapConfigConstants.TERMINAL_ADD_URL);
        url.append("?");
        url.append("key="+mapKey);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("name="+name);
        ResponseEntity<String> Terminal = restTemplate.postForEntity(url.toString(), null, String.class);
        //拼接返回数据
        String body = Terminal.getBody();
        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        String tid = data.getString("tid");

        TerminalResponse terminalResponse = new TerminalResponse();
        terminalResponse.setTid(tid);
        return ResponseResult.success(terminalResponse);
    }
}
